package com.hckjsec.common.utils;

import org.gavaghan.geodesy.Ellipsoid;
import org.gavaghan.geodesy.GeodeticCalculator;
import org.gavaghan.geodesy.GeodeticCurve;
import org.gavaghan.geodesy.GlobalCoordinates;

import java.text.DecimalFormat;

//根据经纬度计算距离
public class DistanceCalculation {
    public static double getDistance(GlobalCoordinates gpsFrom, GlobalCoordinates gpsTo, Ellipsoid ellipsoid){

        //创建GeodeticCalculator，调用计算方法，传入坐标系、经纬度用于计算距离
        GeodeticCurve geoCurve = new GeodeticCalculator().calculateGeodeticCurve(ellipsoid, gpsFrom, gpsTo);
        return geoCurve.getEllipsoidalDistance();
    }
    public static double isTrue(String lon1Str,String lat1Str,String lon2Str,String lat2Str){
        double lon1=StringUtils.StringToDouble(lon1Str);
        double lat1=StringUtils.StringToDouble(lat1Str);
        double lon2=StringUtils.StringToDouble(lon2Str);
        double lat2=StringUtils.StringToDouble(lat2Str);
        //我需要传入的经度 维度都小数点后保留6位
        //lat1，lon1为第一个人的维度和经度;lat2，lon2为第一个人的维度和经度
        GlobalCoordinates source = new GlobalCoordinates(lat1, lon1);
        GlobalCoordinates target = new GlobalCoordinates(lat2, lon2);
        //Sphere坐标系计算结果
        double distance = getDistance(source, target, Ellipsoid.Sphere)/1000;
        DecimalFormat df = new DecimalFormat("#.00");
        return Double.valueOf(df.format(distance)).doubleValue();
    }
}
